package ar.com.fiuba.ElectronicCircuitAnalyst.infrastructure.db4o.repository;

import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.Circuit;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.api.repository.CircuitRepository;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.exceptions.CircuitElementNotFoundException;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.exceptions.InvalidCircuitElementException;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.impl.CircuitImpl;
import ar.com.fiuba.ElectronicCircuitAnalyst.domain.specs.CircuitDummySpecification;

import com.db4o.ObjectSet;

public class CircuitRepositoryImpl extends RepositoryImpl<Circuit> implements CircuitRepository {

	public CircuitRepositoryImpl() {
		super(Circuit.class);
	}

	public Circuit getCircuitByName(String name) throws CircuitElementNotFoundException {

		Circuit circuit = null;
		try {
			circuit = new CircuitImpl(name, null, null, null, new CircuitDummySpecification());
		} catch (InvalidCircuitElementException e) {
			// Yet another unreachable block of code...
		}
		ObjectSet<Circuit> circuits = getDb().queryByExample(circuit);

		if (circuits.size() == 0) {
			throw new CircuitElementNotFoundException("Circuit with name " + name + " not found");
		}

		return (Circuit) circuits.get(0);
	}

}
